//
//  TrailerViewController.m
//  Movie
//
//  Created by cjy on 16/3/28.
//  Copyright © 2016年 HY. All rights reserved.
//

#import "TrailerViewController.h"
#import "MHNetWorkTask.h"
#import "TrailerTableViewCell.h"
#import "ModelTrailer.h"
#import "SVProgressHUD.h"
#import "MusicPlayerViewController.h"
#import <MJRefresh.h>
#import "UIScrollView+JFRefresh.h"

@interface TrailerViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,retain)NSMutableArray *array;
@property (nonatomic,strong)UITableView *tableView;

@property (nonatomic, retain) NSString *str;
@property (nonatomic, assign) NSInteger pag;
@property (nonatomic, assign) NSInteger i;

@end

@implementation TrailerViewController



- (void)viewWillAppear:(BOOL)animated{
    
    self.tabBarController.tabBar.hidden = NO;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [SVProgressHUD show];
    [SVProgressHUD setBackgroundColor:[UIColor grayColor]];
    [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeBlack];
    self.automaticallyAdjustsScrollViewInsets = NO;

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target: self action:@selector(presentLeftMenuViewController:)];
    [self createtableView];

    self.array = [NSMutableArray array];
    self.str = [[NSString alloc] init];
    
    self.str = @"http://mapi.yinyuetai.com/video/list.json?D-A=0&area=POP_ALL&offset=0&size=20";
    self.pag = 20;
    self.i = 1;
    
    __weak typeof(self) weakSelf = self;
    [self.tableView addHeaderWithAction:^{
        [weakSelf headerActionOpration];
    }customControl:^(JFDrawTextView *drawView) {
        drawView.refreshText = @"JuJu-vMoive";
        drawView.textColor = [UIColor colorWithRed:0.641 green:1.000 blue:1.000 alpha:1.000];
    }];
    
    //下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            self.str = [NSString stringWithFormat:@"http://mapi.yinyuetai.com/video/list.json?D-A=0&area=POP_ALL&offset=0&size=20"];
            self.pag = 20;
            NSLog(@"刷新完毕");
            
            [self.tableView.mj_header endRefreshing];
        });
    }];
    
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    
    //上拉加载
    
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            self.str = [NSString stringWithFormat:@"http://mapi.yinyuetai.com/video/list.json?D-A=0&area=POP_ALL&offset=%ld&size=20",self.pag * self.i];
                        
            self.i++;

            [self handle:self.str];
            [self.tableView reloadData];
            
        });
    }];
    
    [self handle:self.str];
}


- (void)headerActionOpration{
    [self.tableView performSelector:@selector(endHeaderRefresh) withObject:nil afterDelay:4];
}

- (void)handle:(NSString *)str{
    // 数据加载完成 加载圈消失
    [SVProgressHUD dismiss];
    
    self.str = str;
    
    NSMutableDictionary *dic =@{@"App-Id" : @"10101027",
                                @"Device-Id" : @"616b51f3e3729568db23640554991556",
                                @"Device-V" : @"aVBob25lIE9TXzkuMi4xXzY0MCoxMTM2XzEwMDAwMTAwMF9pUGhvbmUgNVM="}.mutableCopy;
    
    [MHNetWorkTask getWithURL:self.str withParameter:nil withHttpHeader:dic withResponseType:ResponseTypeJSON withSuccess:^(id result) {
        
        NSArray *arrvideos = [result objectForKey:@"videos"];
        
        for (NSDictionary *dicvideos in arrvideos) {
            ModelTrailer *model = [[ModelTrailer alloc]init];
            
            model.descrip = [dicvideos objectForKey:@"description"];
            model.ida = [dicvideos objectForKey:@"id"];
            model.url = [dicvideos objectForKey:@"url"];
            [model setValuesForKeysWithDictionary:dicvideos];
            
            [self.array addObject:model];
      
        }
   
        [self.tableView reloadData];
    } withFail:^(NSError *error) {
        
        
    }];
    
    [self.tableView.mj_footer endRefreshing];
}

- (void)createtableView{
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    
    [self.view addSubview:self.tableView];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor colorWithWhite:0.118 alpha:1.000];
    self.tableView.separatorStyle = UITableViewCellAccessoryNone;
    self.tableView.showsVerticalScrollIndicator = NO;
    [self.tableView registerClass:[TrailerTableViewCell class] forCellReuseIdentifier:@"pool"];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    TrailerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
    
    MusicModel *model = self.array[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell passModel:model];
    
    return cell;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return [UIScreen mainScreen].bounds.size.height / 3 - 20;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MusicPlayerViewController *player = [[MusicPlayerViewController alloc]init];
    
    ModelTrailer *model = self.array[indexPath.row];
    
    player.url = model.shdUrl;
    player.posterPic = model.posterPic;
    player.desp = model.descrip;
    player.arr = self.array[indexPath.row];
    player.ida = [model.ida integerValue];
    player.model = self.array[indexPath.row];
    player.arrS = self.array[indexPath.row];
    [self.navigationController pushViewController:player animated:YES];
  
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
